#ifndef SERVER_HPP
#define SERVER_HPP
#include <xnetwork/XTcpServer.hpp>
#include<xsignal/sigReciver.hpp>
#include<xsignal/signal.hpp>

class TcpServer:public sigReciver{
public:
    TcpServer(std::string ip,unsigned int port){
        m_socket = new XTcpServer({ip,port});
        AttachSignalSlot();
    }
    ~TcpServer(){
        delete m_socket;
    }

    void AttachSignalSlot()
    {
         SIG::connect(m_socket,&XTcpServer::sig_connect,[](SOCKET s){
            std::cout<<"TcpServer: new socket eatablished! socket:"<<s<<std::endl;
        });

        SIG::connect(m_socket,&XTcpServer::sig_disconnect,[](SOCKET s){
            std::cout<<"TcpServer: socket disconnect! socket:"<<s<<std::endl;
        });

        SIG::connect(m_socket,&XTcpServer::sig_read,this,&TcpServer::slotRead);
    }

private:
    XTcpServer *m_socket = nullptr;

    void slotRead(SOCKET s,char *buf,int len){
        std::cout <<"TcpServer: slotRead socket:"<<s<<" buf:"<< buf<<std::endl;
        m_socket->write(s,buf,len);
    }
};
#endif